//
//  LocationView.m
//  huashiedu
//
//  Created by oceans on 2018/8/27.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "LocationView.h"
#import "LocationModel.h"

@interface LocationView ()

@property (strong, nonatomic) UILabel *titleLabel;
@end

@implementation LocationView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 12, 20, 20)];
    imageView.image = [UIImage imageNamed:@"location"];
    [self addSubview:imageView];
    
    NSString *string = [LocationModel shared].areaName;
//    CGRect rect = [string boundingRectWithSize:CGSizeMake(70, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15.0]} context:nil];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(24, 12, 46, 20)];
    _titleLabel.font = [UIFont systemFontOfSize:15.0];
    _titleLabel.textColor = COLOR(49, 49, 49, 1);
    _titleLabel.text = string;
    [self addSubview:_titleLabel];
    
    //CGRectGetMaxX(_titleLabel.frame) + 16.0
    self.frame = CGRectMake(0, 0, 70, 44);
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(locationModelChanged) name:locationModelChangedNotification object:nil];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes)];
    [self addGestureRecognizer:tap];
}

- (void)locationModelChanged {
    
    NSString *string = [LocationModel shared].areaName;
    if (string == nil || string.length == 0) {
        return;
    }
//    CGRect rect = [string boundingRectWithSize:CGSizeMake(70, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15.0]} context:nil];
    _titleLabel.frame = CGRectMake(24, 12, 46, 20);
    _titleLabel.text = string;
    self.frame = CGRectMake(0, 0, 70, 44);
//    [_delegate locationViewWidthChanged];
}

- (void)tapGes {
    [_delegate locationViewTaped];
}

+ (LocationView *)locationView {
    
    LocationView *view = [[LocationView alloc] initWithFrame:CGRectZero];
    return view;
}

@end
